package com.intellagric.controller.facility;

import com.intellagric.common.bean.LayuiDataGridResult;
import com.intellagric.common.bean.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.interfaces.facility.FacilityUsedService;
import com.intellagric.pojo.FacilityUsed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;

@Controller
@RequestMapping("/admin")
public class FacilityUsedController {

    @Autowired
    private FacilityUsedService facilityUsedService;

    @RequestMapping("/facilityUsed/add")
    @ResponseBody
    public ResponseMessage addFacilityUsed(FacilityUsed FacilityUsed){
        FacilityUsed.setId(UUIDUtils.getID());
        return facilityUsedService.insertFacilityUsed(FacilityUsed);
    }

    @RequestMapping("/facilityUsed/edit")
    @ResponseBody
    public ResponseMessage editFacilityUsed(FacilityUsed FacilityUsed){
        return facilityUsedService.editFacilityUsed(FacilityUsed);
    }

    @RequestMapping("/facilityUsed/delete")
    @ResponseBody
    public ResponseMessage deleteFacilityUsed(String id){
        return facilityUsedService.deleteFacilityUsed(id);
    }

    @RequestMapping("/facilityUsed/batchdelete")
    @ResponseBody
    public LayuiDataGridResult batchDelete(@RequestParam("ids[]") String[] ids){
        return facilityUsedService.batchDeleteFacilityUsed(ids);
    }


    @RequestMapping("/facilityUsed/getUsedList")
    @ResponseBody
    public LayuiDataGridResult getUsedList(int page,int limit){
        return  facilityUsedService.getList(page,limit);
    }
    @RequestMapping("/facilityUsed/getUsedListByCatId")
    @ResponseBody
    public LayuiDataGridResult getUsedListByCategory(int page,int limit,String categoryId){
        return  facilityUsedService.getListByCategoryId(page,limit,categoryId);
    }
    @RequestMapping("/facilityUsed/getFacilityUsed")
    @ResponseBody
    public FacilityUsed getFacilityUsed(String FacilityUsedId){
        return (FacilityUsed) facilityUsedService.selectFacilityUsedById(FacilityUsedId).getData().get("usedBean");
    }

    @RequestMapping("/facilityUsed/search")
    @ResponseBody
    public LayuiDataGridResult search(int page, int limit, String keyword){
        return facilityUsedService.getFacilityUsedByKeyword(page,limit,keyword);
    }


    /**
     * 查找预约物品列表
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/facilityUsed/bookedList")
    @ResponseBody
    public LayuiDataGridResult bookedList(int page,int limit){
        return facilityUsedService.getBookedList(page,limit);
    }

    /**
     * 根据分类查找预约物品列表
     * @param page
     * @param limit
     * @param categoryId
     * @return
     */
    @RequestMapping("/facilityUsed/getBookedListByCatId")
    @ResponseBody
    public LayuiDataGridResult getBookedListByCategory(int page,int limit,String categoryId){
        return  facilityUsedService.getBookedListByCategoryId(page,limit,categoryId);
    }

    /**
     * 插入预定申请
     * @param FacilityUsed
     * @return
     */
    @RequestMapping("/facilityUsed/bookAdd")
    @ResponseBody
    public ResponseMessage addFacilityBook(FacilityUsed FacilityUsed){
        FacilityUsed.setId(UUIDUtils.getID());
        return facilityUsedService.insertFacilityBooked(FacilityUsed);
    }
    /**
     * 归还设备
     * @param FacilityUsedId
     * @return
     */
    @RequestMapping("/facilityUsed/bookReturn")
    @ResponseBody
    public LayuiDataGridResult addFacilityBook(String FacilityUsedId){
        return facilityUsedService.bookReturn(FacilityUsedId);
    }
    /**
     * 批量归还设备
     * @param FacilityUsedIds
     * @return
     */
    @RequestMapping("/facilityUsed/batchBookReturn")
    @ResponseBody
    public LayuiDataGridResult addFacilityBook(@RequestParam("ids[]") String[] FacilityUsedIds){
        return facilityUsedService.batchBookReturn(FacilityUsedIds);
    }

    /**
     * 获取日期
     * @return
     */
    @RequestMapping("/facilityUsed/getRecentDate")
    @ResponseBody
    public String[] getRecentDate(){
        String[] strs=facilityUsedService.getRecentDate();
        return strs;
    }
    /**
     * 获取数据
     * @return
     */
    @RequestMapping("/facilityUsed/getRecentData")
    @ResponseBody
    public long[] getRecentData(){
        try {
            return facilityUsedService.getRecentData();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
